﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace WooEngine.Utility.Other
{
    public class FPS : MonoBehaviour
    {
        public string m_FPS;
        float _updateInterval = 1f;//设定更新帧率的时间间隔为1秒  
        float _accum = .0f;//累积时间  
        int _frames = 0;//在_updateInterval时间内运行了多少帧  
        float _timeLeft;
        float p;

        void Start()
        {
            _timeLeft = _updateInterval;
            p = Screen.width / 720;
        }

        // Update is called once per frame  
        void Update()
        {
            _timeLeft -= Time.deltaTime;
            //Time.timeScale可以控制Update 和LateUpdate 的执行速度,  
            //Time.deltaTime是以秒计算，完成最后一帧的时间  
            //相除即可得到相应的一帧所用的时间  
            _accum += Time.timeScale / Time.deltaTime;
            ++_frames;//帧数  

            if (_timeLeft <= 0)
            {
                float fps = _accum / _frames;
                //Debug.Log(_accum + "__" + _frames);  
                string fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数  
                m_FPS = fpsFormat;

                _timeLeft = _updateInterval;
                _accum = .0f;
                _frames = 0;
            }
        }

        private void OnGUI()
        {
            GUIStyle style = new GUIStyle
            {
                border = new RectOffset(10, 10, 10, 10),
                fontSize = 50,
                fontStyle = FontStyle.BoldAndItalic,
            };

            GUI.Label(new Rect(Screen.width / 2 - 100 * p, 0, 200 * p, 80 * p), m_FPS, style);
        }
    }
}